package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    OrdersMapper ordersMapper;

    /**
     * 查询营业额
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO TurnoverReport(LocalDate begin, LocalDate end) {
        List<LocalDate> dateListTime = new ArrayList<>();
        while (!begin.equals(end)) {
            dateListTime.add(begin);
            begin = begin.plusDays(1);
        }
        dateListTime.add(end);
        String dateList = StringUtils.join(dateListTime, ",");

        List<Double> moneyOfDay = new ArrayList<>();
        //select sum(amount) from orders where order_time &gt;= Min AND
        for (LocalDate localDate : dateListTime) {
            LocalDateTime min = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime max = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", min);
            map.put("end", max);
            map.put("status", Orders.COMPLETED);
            Double money = ordersMapper.selectRangeOfDay(map);
            money = money == null ? 0.0 : money;
            moneyOfDay.add(money);
        }
        String turnoverList = StringUtils.join(moneyOfDay, ",");
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(dateList)
                .turnoverList(turnoverList)
                .build();
        return turnoverReportVO;

    }
}
